package com.spark.mooc.ch5_rdd.part04_cases

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @description:
 * @time: 2020/11/26 23:49
 * @author: lhy
 */
object TopN {
    def main(args: Array[String]): Unit = {
        val conf: SparkConf = new SparkConf().setAppName("TopN").setMaster("local")
        val sc = new SparkContext(conf)
        sc.setLogLevel("ERROR")     //设置日志格式级别
        val lines: RDD[String] = sc.textFile("input/rdd/topN/*", 2)
        var num = 0     //排序用的变量
//        val result: Unit = lines.filter(line => (line.trim().length > 0) && (line.split(",").length == 4))
//                                .map(_.split(",")(2))
//                                .map(x => (x.toInt," "))
//                                .sortByKey(ascending = false)
//                                .map(x => x._1).take(5)
//                                .foreach(x => {
//                                    num += 1
//                                    println(num +"\t"+x)
//                                })
        val results: Array[Int] = lines.filter(line => (line.trim().length > 0) && (line.split(",").length == 4))
                                       .map(_.split(",")(2))
                                       .map(x => (x.toInt," "))
                                       .sortByKey(ascending = false)
                                       .map(_._1)
                                       .take(5)
        results.foreach(x => {
            num += 1
            println(num + "\t" + x)
        })
    }
}
